package school.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import school.domain.Comment;
import school.domain.News;
import school.domain.Route;
import school.domain.User;
import school.mapper.CommentMapper;
import school.mapper.NewsMapper;
import school.mapper.RouteMapper;
import school.mapper.UserMapper;
import school.support.BaseController;
import school.support.util.ParamUtils;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class IndexController extends BaseController {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private NewsMapper newsMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private RouteMapper routeMapper;

    /**
     * 首页
     *
     * @param model
     * @param pageNum
     * @return
     */
    @RequestMapping({"index"})
    public String index(Model model, @RequestParam(defaultValue = "1") Integer pageNum) {
        PageHelper.startPage(pageNum, 10);
        PageInfo<News> pageInfo = new PageInfo<>(newsMapper.selectList(null), 5);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("requestUrl", request.getRequestURI() + "?" + ParamUtils.params2String(request));

        /**
         * 出行计划
         */
        List<Route> routeList = routeMapper.selectList(null);
        model.addAttribute("routeList", routeList);

        return "index";
    }

    /**
     * 资讯详情
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("detail/{id}")
    public String detail(@PathVariable Integer id, Model model) {
        News news = newsMapper.selectById(id);
        Comment comment = new Comment();
        comment.setNewsid(id);
        List<Comment> commentList = commentMapper.selectList(comment);
        model.addAttribute(news);
        model.addAttribute("commentList", commentList);
        return "detail";
    }


    /**
     * 出行计划详情
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("route/{id}")
    public String route(@PathVariable Integer id, Model model) {
        if(isLogin()){
            Integer i = routeMapper.selectOrder(loginUser().getId(), id);
            model.addAttribute("st",i);
        }
        Route route = routeMapper.selectById(id);
        model.addAttribute(route);
        return "route";
    }


    /**
     * 登录
     *
     * @return
     */
    @GetMapping("login")
    public String login() {
        return "login";
    }

    /**
     * 登录
     *
     * @param username
     * @param password
     * @return
     */
    @PostMapping("login")
    public String login(String username, String password) {
        User user = userMapper.login(username, password);
        if (null == user) return "redirect:login.do";
        session.setAttribute("sessionUser", user);
        return "redirect:index.do";
    }

    /**
     * 注册
     *
     * @return
     */
    @GetMapping("register")
    public String register() {
        return "register";
    }

    /**
     * 注册
     *
     * @param username
     * @param password
     * @return
     */
    @PostMapping("register")
    public String register(String username, String password) {
        userMapper.register(username, password);
        return "redirect:login.do";
    }

    /**
     * 退出登录
     *
     * @param session
     * @return
     */
    @GetMapping("logout")
    public String logout(HttpSession session) {
        session.removeAttribute("sessionUser");
        return "redirect:index.do";
    }
}
